package cn.com.controller.operationController;


import cn.com.Enum.UserAuth;
import cn.com.annotation.Auth;
import cn.com.constant.Code;
import cn.com.db.model.Goods;
import cn.com.db.service.GoodsService;
import cn.com.util.DataResult;
import cn.com.util.JsonUtil;
import cn.com.util.TimeUtil;
import cn.com.util.UidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;


@Auth(UserAuth.SELLER)
@RestController
@RequestMapping("/operation/goods")
public class GoodsOperationController {

    @Autowired
    GoodsService goodsService;


    @RequestMapping(method = RequestMethod.POST)
    public String getStoreGoods(@RequestParam("storeId")String storeId,
                                @RequestParam("page")Integer page,
                                @RequestParam("count")Integer count)
    {
        DataResult dataResult=new DataResult(page,count);
        List<Goods> list=goodsService.getStoreGoods(storeId, page, count);
        Integer total=goodsService.countStoreGoods(storeId);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("获取成功");
        dataResult.setData(list);
        dataResult.setTotal(total);
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String createGoods(Goods goods){
        DataResult dataResult=new DataResult();
        goods.setId(UidUtil.createUUID());
        goods.setDel(false);
        goods.setAlive(true);
        goods.setCreated_at(TimeUtil.getNowTimestamp());
        goods.setUpdated_at(TimeUtil.getNowTimestamp());
        goodsService.createGoods(goods);
        dataResult.setData(goods);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("添加成功");
        return JsonUtil.toJsonString(dataResult);
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String updateGoods(Goods goods){
        DataResult dataResult=new DataResult();
        if (!goodsService.isExist(goods.getId())){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("商品不存在,或商品已被删除");
            return JsonUtil.toJsonString(dataResult);
        }
        goodsService.updateGoods(goods);
        dataResult.setData(goods);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("成功编辑");
        return JsonUtil.toJsonString(dataResult);
    }


    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public String deleteGoods(@RequestParam("goodsId") String goodsId){
        DataResult dataResult=new DataResult();
        if (!goodsService.isExist(goodsId)){
            dataResult.setCode(Code.NOT_FOUND);
            dataResult.setMessage("商品不存在,或商品已被删除");
            return JsonUtil.toJsonString(dataResult);
        }
        Goods goods=goodsService.getGoodsById(goodsId);
        goods.setDel(true);
        goodsService.updateGoods(goods);
        dataResult.setData(goods);
        dataResult.setCode(Code.SUCCESS);
        dataResult.setMessage("删除成功");
        return JsonUtil.toJsonString(dataResult);
    }





}
